package com.gen.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gen.service.ILikeService;
import com.zzx.entity.Like;


@Controller
public class ShoppingCartController {

	@Autowired
	private HttpSession Session;
	@Autowired
	private ILikeService likeService;
	
	@RequestMapping("/shoppingCart.do")
	public String ShoppingCart() {
		return "shoppingCart";
	}
	
	@RequestMapping("/removeFromCart.do")
	public ModelAndView removeFromCart(@RequestParam("pid") Integer pid) {
		ModelAndView modelAndView = new ModelAndView();
		Map<Integer, Like> shoppingCart =(HashMap<Integer, Like>) Session.getAttribute("shoppingCart");
		shoppingCart.remove(pid);
		modelAndView.addObject("shoppingCart", shoppingCart);
		modelAndView.setViewName("shoppingCart");
		return modelAndView;
	}
	@RequestMapping("/addGoodsToCart.do")
	@ResponseBody
	public int addGoodsToCart(@RequestParam("pid") Integer pid) {
		Map<Integer, Like> shoppingCart =(HashMap<Integer, Like>) Session.getAttribute("shoppingCart");
		Like like = likeService.selectLikeFromLikeByPid(pid);
		shoppingCart.put(pid, like);
		return 1;
	}
	
}
